package com.sixbro.caicaitv.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sixbro.caicaitv.R;
import com.sixbro.caicaitv.bean.ChannelBean;
import com.sixbro.caicaitv.common.Consts;
import com.sixbro.caicaitv.storage.SPHelper;
import com.sixbro.caicaitv.widget.CcLivePlayer;
import com.sixbro.caicaitv.widget.TvRecyclerView;

public class TvActivity extends AppCompatActivity {

    private int mIndexCur;
    private BaseQuickAdapter mApdater;
    private CcLivePlayer ccLivePlayer;
    private TvRecyclerView rvChannel;
    private LinearLayout llytSelectChannel;
    private ChannelBean mChannelBeanCur;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);// 保持屏幕高亮
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tv);
        init();
    }

    private void init() {
        bindViews();
        mIndexCur = SPHelper.get("index", 0);
        rvChannel.setHasFixedSize(true);
        rvChannel.setLayoutManager(new GridLayoutManager(this, 7));
        rvChannel.setAdapter(mApdater = new BaseQuickAdapter<ChannelBean, BaseViewHolder>(R.layout.itemview_channel) {

            @Override
            protected void convert(BaseViewHolder helper, ChannelBean item) {
                AppCompatTextView tvChannel = helper.getView(R.id.tv_channel);
                tvChannel.setText(item.getName());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showHideSelectPanel(false);
                        mIndexCur = helper.getAdapterPosition();
                        setChannelAndPaly(true);
                    }
                });
            }
        });
        mApdater.setNewData(Consts.DEFAULT_CHANNELS);
        ccLivePlayer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showHideSelectPanel(!isSelectPanelShowing());
            }
        });
        setChannelAndPaly(true);
    }

    private void setChannelAndPaly(boolean immediately) {
        mIndexCur = mIndexCur % Consts.DEFAULT_CHANNELS.size();
        SPHelper.set("index", mIndexCur);
        mChannelBeanCur = Consts.DEFAULT_CHANNELS.get(mIndexCur);
        ccLivePlayer.startPlay(mChannelBeanCur, immediately);
    }

    //选频道面板是否显示
    private void showHideSelectPanel(boolean toShow) {
        if (toShow) {
            llytSelectChannel.setVisibility(View.VISIBLE);
            rvChannel.requestFocus();
            int foucsIndex = mIndexCur > rvChannel.getChildCount() - 1 ? rvChannel.getChildCount() - 1 : mIndexCur;
            rvChannel.getChildAt(foucsIndex).requestFocus();
            rvChannel.scrollToPosition(mIndexCur);
        } else {
            llytSelectChannel.setVisibility(View.INVISIBLE);
        }
    }

    private boolean isSelectPanelShowing() {
        return llytSelectChannel.getVisibility() == View.VISIBLE;
    }

    //频道前进后退
    private void forwardOrBackward(boolean forward) {
        if (forward) {
            mIndexCur++;
        } else {
            if (mIndexCur == 0) {
                mIndexCur = Consts.DEFAULT_CHANNELS.size();
            }
            mIndexCur--;
        }
        setChannelAndPaly(false);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_ENTER:     //确定键enter
            case KeyEvent.KEYCODE_DPAD_CENTER:
                if (isSelectPanelShowing()) {
                    showHideSelectPanel(false);
                    mIndexCur = rvChannel.getSelectedPosition();
                    setChannelAndPaly(true);
                } else {
                    showHideSelectPanel(true);
                }
                break;
            case KeyEvent.KEYCODE_INFO:    //info键
            case KeyEvent.KEYCODE_SETTINGS: //设置键
                showHideSelectPanel(!isSelectPanelShowing());
                break;
            case KeyEvent.KEYCODE_BACK:    //返回键
                if (isSelectPanelShowing()) {
                    showHideSelectPanel(false);
                    return true;
                }
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT: //向左键
            case KeyEvent.KEYCODE_DPAD_DOWN:   //向下键
            case KeyEvent.KEYCODE_PAGE_DOWN:   //向下翻页键
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            case KeyEvent.KEYCODE_CHANNEL_DOWN:
                /*    实际开发中有时候会触发两次，所以要判断一下按下时触发 ，松开按键时不触发
                 *    exp:KeyEvent.ACTION_UP
                 */
                if (!isSelectPanelShowing()) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        forwardOrBackward(false);
                        return true;
                    }
                }
                break;
            case KeyEvent.KEYCODE_DPAD_UP:   //向上键
            case KeyEvent.KEYCODE_DPAD_RIGHT:  //向右键
            case KeyEvent.KEYCODE_PAGE_UP:     //向上翻页键
            case KeyEvent.KEYCODE_MEDIA_NEXT:
            case KeyEvent.KEYCODE_CHANNEL_UP:
                if (!isSelectPanelShowing()) {
                    forwardOrBackward(true);
                    return true;
                }
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:   //调大声音键
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN: //降低声音键
                break;
            case KeyEvent.KEYCODE_VOLUME_MUTE: //禁用声音
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void bindViews() {
        ccLivePlayer = findViewById(R.id.cc_live_player);
        rvChannel = findViewById(R.id.rv_channel);
        llytSelectChannel = findViewById(R.id.llyt_select_channel);
    }
}